#!/usr/bin/env ruby

ENV['RAILS_ENV'] ||= 'production'
require_relative '../config/environment'

name, number, email = ARGV

if [name, number, email].any?(&:blank?)
  abort "Usage: #{$PROGRAM_NAME} [GEM_NAME=hola] [VERSION=all] [AS=email]"
end

rubygem = Rubygem.find_by_name!(name)

versions = begin
  if number == "all"
    rubygem.versions
  else
    [rubygem.versions.find_by_number!(number)]
  end
end

owner = User.find_by_email!(email)

require 'highline'
terminal = HighLine.new
confirm  = terminal.ask "Permanently delete #{name} with #{versions.size} versions? (y/N)"

if confirm.casecmp("Y") || confirm.casecmp("YES")
  versions.each do |version|
    owner.deletions.create!(version: version)
  end

  puts "#{name} permadeleted."
else
  abort "No problem! #{name} lives on."
end
